<template>
  <div class="password">
    <div class="passwordBox">
      <h2>修改密码</h2>
      <el-form :rules="rules" ref="form">
        <el-form-item>
          <el-input
            v-model="userName"
            style="width: 300px"
            placeholder="用户名"
            clearable
          />
        </el-form-item>
        <el-form-item prop="oldPassword">
          <el-input
            v-model="oldPassword"
            style="width: 300px"
            type="password"
            placeholder="旧密码"
            show-password
          />
        </el-form-item>
        <el-form-item
          prop="newPassword"
        >
          <el-input
            v-model="newPassword"
            style="width: 300px"
            type="password"
            placeholder="新密码"
            show-password
          />
        </el-form-item>
        <el-form-item prop="confirmPassword">
          <el-input
            v-model="confirmPassword"
            style="width: 300px"
            type="password"
            placeholder="确认密码"
            show-password
          />
        </el-form-item>
      </el-form>
      <div class="btnBox">
        <div class="passwordBtn">
          <el-button
            type="primary"
            style="width: 100px; margin-bottom: 20px"
            @click="handleChange"
            >确认修改</el-button
          >
          <RouterLink to="/login" style="text-decoration: none; color: #409eff"
            >返回登录</RouterLink
          >
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useUserDateStore } from '@/stores/user'
import { changePasswordHttp } from '@/request/index'
import { ElMessage } from 'element-plus'
import 'element-plus/theme-chalk/el-message.css'

const open1 = () => {
  ElMessage({
    message: '修改密码成功',
    type: 'success',
    plain: true,
  })
}
const open2 = () => {
  ElMessage({
    message: '新密码不能与旧密码相同',
    type: 'warning',
    plain: true,
  })
}
const open3 = () => {
  ElMessage({
    message: '两次密码输入不一致',
    type: 'error',
    plain: true,
  })
}
const open4 = () => {
  ElMessage({
    message: '修改密码失败,请重试',
    type: 'error',
    plain: true,
  })
}
const open5 = () => {
  ElMessage({
    message: '修改密码时发生错误，请稍后再试',
    type: 'error',
    plain: true,
  })
}
const open6 = () => {
  ElMessage({
    message: '用户名不能为空',
    type: 'error',
    plain: true,
  })
}
const userName = ref('') //用户名
const oldPassword = ref('') //旧密码
const newPassword = ref('') //新密码
const confirmPassword = ref('') //确认密码
const { userData } = useUserDateStore()

// 密码规则
const validatePass = (rule, value, callback) => {
  if (oldPassword.value === '') {
    callback(new Error('请输入原密码'))
  }
}
const validatePass1 = (rule, value, callback) => {
  if (newPassword.value === '') {
    callback(new Error('请输入新密码'))
  } else if (newPassword.value === oldPassword.value) {
    callback(new Error('新密码不能与旧密码相同'))
  } else {
    callback()
  }
}
const validatePass2 = (rule, value, callback) => {
  if (confirmPassword.value === '') {
    callback(new Error('请再次输入密码'))
  } else if (confirmPassword.value !== newPassword.value) {
    callback(new Error('两次密码输入不一致'))
  } else {
    callback()
  }
}

const rules = {
  oldPassword: [{ validator: validatePass, trigger: 'blur' }],
  newPassword: [{ validator: validatePass1, trigger: 'blur' }],
  confirmPassword: [{ validator: validatePass2, trigger: 'blur' }],
}

const handleChange = () => {
  if (userName.value === '') {
    open6()
    return
  }
  if (oldPassword.value === newPassword.value) {
    open2()
    return
  }
  if (newPassword.value !== confirmPassword.value) {
    open3()
    return
  }
  try {
    changePasswordHttp({
      username: userName.value,
      password: oldPassword.value,
      newPassword: newPassword.value,
      type: userData.type,
    }).then((res) => {
      if (res.code === 200) {
        oldPassword.value = newPassword.value
        open1()
      } else {
        open4()
      }
    })
  } catch (error) {
    open5()
  }
}
</script>

<style lang="scss" scoped>
.password {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-image: url('@/assets/images/bg.jpg');
  .passwordBox {
    width: 450px;
    padding: 20px;
    margin: auto;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    gap: 25px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.4);
    border-radius: 10px;
    background-color: #fff;
    h2 {
      text-align: center;
      font-size: 30px;
      color: #333;
      margin-bottom: 20px;
    }
    .passwordBtn {
      .btnBox {
        display: flex;
      }
      display: flex;
      justify-content: center;
      flex-direction: column;
      align-items: center;
      gap: 10px;
      margin-top: 20px;
    }
  }
}
</style>
